package com.haobo.dao.impl;

import com.haobo.dao.RoleDao;
import com.haobo.pojo.Role;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * ClassName:RoleDaoImpl
 * Package:com.haobo.dao.impl
 * Description:
 *
 * @Date:2021/5/28 18:21
 * @Author:haobo
 */
public class RoleDaoImpl implements RoleDao {

    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Role> findAll() {
        List<Role> roleList = jdbcTemplate.query("select * from sys_role",new BeanPropertyRowMapper<Role>(Role.class));
        return roleList;
    }

    @Override
    public void addRole(Role role) {
        jdbcTemplate.update("insert into sys_role value(?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    @Override
    public void delRole(Long roleId) {
        jdbcTemplate.update("delete from sys_role where id=?",roleId);
    }

    @Override
    public List<Role> findRoleByUserId(Long id) {
        List<Role> roleList = jdbcTemplate.query("select * from sys_role r,sys_user_role ur where r.id=ur.roleId and ur.userId=?", new BeanPropertyRowMapper<Role>(Role.class), id);
        return roleList;
    }

    @Override
    public void delUserRoleRel(Long roleId) {
        jdbcTemplate.update("delete from sys_user_role where roleId=?",roleId);
    }
}
